<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="客户端名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户端名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户端描述" prop="clientDescription">
        <el-input
          v-model="queryParams.clientDescription"
          placeholder="请输入客户端描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户端状态" prop="clientStatus">
        <el-select
          v-model="queryParams.clientStatus"
          placeholder="请选择客户端状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in clientStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户端类型" prop="clientType">
        <el-select
          v-model="queryParams.clientType"
          placeholder="请选择客户端类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in clientTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['client:mes_client_all:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['client:mes_client_all:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['client:mes_client_all:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['client:mes_client_all:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="mes_client_allList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="中心ID"
        align="center"
        prop="clientId"
        v-if="false"
      />
      <el-table-column label="客户端名称" align="center" prop="clientName" />
      <el-table-column
        label="客户端描述"
        align="center"
        prop="clientDescription"
      />
      <el-table-column
        label="客户端状态"
        align="center"
        prop="clientStatus"
        :formatter="clientStatusFormat"
      />
      <el-table-column
        label="客户端类型"
        align="center"
        prop="clientType"
        :formatter="clientTypeFormat"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleMoreSet(scope.row)"
            v-hasPermi="['client:mes_client_all:moreset']"
            >更多配置</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['client:mes_client_all:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['client:mes_client_all:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户端管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="客户端名称" prop="clientName">
          <el-input v-model="form.clientName" placeholder="请输入客户端名称" />
        </el-form-item>

        <el-form-item label="客户端状态">
          <el-radio-group v-model="form.clientStatus">
            <el-radio
              v-for="dict in clientStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="客户端类型" prop="clientType">
          <el-select v-model="form.clientType" placeholder="请选择客户端类型">
            <el-option
              v-for="dict in clientTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户端描述" prop="clientDescription">
          <el-input
            type="textarea"
            v-model="form.clientDescription"
            placeholder="请输入客户端描述"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="客户端注册参数配置"
      :visible.sync="clientVisible"
      width="80%"
      :lock-scroll="false"
    >
      <el-form ref="info" :model="info" label-width="100px">
        <el-row :gutter="20">
          <el-col :lg="8">
            <el-form-item label="客户端名称" prop="clientName">
              <el-input
                v-model="info.clientName"
                placeholder="请输入客户端名称"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :lg="8">
            <el-form-item label="客户端状态">
              <el-radio-group v-model="info.clientStatus">
                <el-radio
                  v-for="dict in clientStatusOptions"
                  disabled
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                  >{{ dict.dictLabel }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :lg="8">
            <el-form-item label="客户端类型" prop="clientType">
              <el-select
                v-model="info.clientType"
                placeholder="请选择客户端类型"
                disabled
              >
                <el-option
                  v-for="dict in clientTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8">
            <el-form-item label="客户端描述" prop="clientDescription">
              <el-input
                type="textarea"
                v-model="info.clientDescription"
                disabled
                placeholder="请输入客户端描述"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-tabs type="border-card">
        <el-tab-pane label="页面分配">
          <!-- 工具区域 -->
          <web-data :clientId="clientId"></web-data>
        </el-tab-pane>
        <el-tab-pane label="客户端使用处">
          <!-- 工具区域 -->
          <client-data :clientId="clientId"></client-data>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMes_client_all,
  getMes_client_all,
  delMes_client_all,
  addMes_client_all,
  updateMes_client_all,
  exportMes_client_all,
} from "@/api/client/mes_client_all";
import clientData from "@/views/components/clientData";
import webData from "@/views/components/webData";
export default {
  name: "Mes_client_all",
  components: { clientData, webData },
  data() {
    return {
      clientVisible: false,
      // 终端CLIENT_ID传值给子组件
      clientId: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户端管理表格数据
      mes_client_allList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 客户端状态字典
      clientStatusOptions: [],
      // 客户端类型字典
      clientTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientName: null,
        clientDescription: null,
        clientStatus: null,
        clientType: null,
        field2: null,
        field3: null,
        field4: null,
      },
      // 表单参数
      form: {},
      info: {},
      // 表单校验
      rules: {
        clientName: [
          { required: true, message: "客户端名称不能为空", trigger: "blur" },
        ],
        clientDescription: [
          { required: true, message: "客户端描述不能为空", trigger: "blur" },
        ],
        clientStatus: [
          { required: true, message: "客户端状态不能为空", trigger: "blur" },
        ],
        clientType: [
          { required: true, message: "客户端类型不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then((response) => {
      this.clientStatusOptions = response.data;
    });
    this.getDicts("mes_client_type").then((response) => {
      this.clientTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询客户端管理列表 */
    getList() {
      this.loading = true;
      listMes_client_all(this.queryParams).then((response) => {
        this.mes_client_allList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 客户端状态字典翻译
    clientStatusFormat(row, column) {
      return this.selectDictLabel(this.clientStatusOptions, row.clientStatus);
    },
    // 客户端类型字典翻译
    clientTypeFormat(row, column) {
      return this.selectDictLabel(this.clientTypeOptions, row.clientType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        clientId: null,
        clientName: null,
        clientDescription: null,
        clientStatus: "0",
        clientType: null,
        createTime: null,
        delflag: null,
        updateBy: null,
        updateTime: null,
        createBy: null,
        field1: null,
        field2: null,
        field3: null,
        field4: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.clientId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户端管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const clientId = row.clientId || this.ids;
      getMes_client_all(clientId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户端管理";
      });
    },
    handleMoreSet(row) {
      this.clientId = row.clientId;
      getMes_client_all(row.clientId).then((response) => {
        this.info = response.data;
        this.clientVisible = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.clientId != null) {
            updateMes_client_all(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addMes_client_all(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const clientIds = row.clientId || this.ids;
      this.$confirm(
        '是否确认删除客户端管理编号为"' + clientIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delMes_client_all(clientIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有客户端管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportMes_client_all(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        })
        .catch(function () {});
    },
  },
};
</script>
